<?php

class CMealsFrontEndUtils {
	
	public static $UNKNOWN_MEMBER = "?";
	
	/**
	 * Find out which member the front end user represents. If no match, return "?".
	 * @return member id as string
	 */
	public static function getCurrentMemberId() {
		$session = JFactory::getSession();
		$member_id = $session->get( "member_id", null, "cmeals" );
		if ($member_id) {
			return $member_id;
		}
		$user = JFactory::getUser();
		if ($user->guest) {
			return self::$UNKNOWN_MEMBER;
		}
		$member_id = CMeals::getMemberIdByUserName($user->username);
		if (!$member_id) {
			return self::$UNKNOWN_MEMBER;
		}
		$session->set( "member_id", $member_id, "cmeals" );
		return $member_id;
	}
	
	/**
	 * Member (display name) of the front end user who is logged in. If no match, return "?".
	 * @return member name as string
	 */
	public static function getCurrentMemberName() {
		$member_id = self::getCurrentMemberId();
		if ($member_id == self::$UNKNOWN_MEMBER) {
			return self::$UNKNOWN_MEMBER;
		}
		$member = CMeals::readMember($member_id);
		if (!$member) { 
			return self::$UNKNOWN_MEMBER;
		}
		return $member->name;
	}
	
	/**
	 * Explodes comma-seperated list to array, trimming each value
	 * @param string $values
	 * @return array
	 */
	public static function explodeAndTrim($values) {
		$valueList = array();
		foreach (explode(",", $values) as $value) {
			$valueList[] = trim($value);
		}
		return $valueList;
	}
	
} 

class CMealsMenu {
	
	private $menuAsString = "";
	
	public function addLink($jtextLabel, $url) {
		$this->menuAsString .= "<span><a href=\"".$url."\">".JText::_($jtextLabel)."</a></span>";
	}
	
	public function addSeparator() {
		$this->menuAsString .= "<span style=\"color:lightgray;font-size:larger\">&nbsp;|&nbsp;</span>";
	}
	
	public function addBigSeparator() {
		$this->menuAsString .= "<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
	}
	
	public function toString() {
		$this->addBigSeparator();
		return $this->menuAsString.JText::_("MEMBER")."&nbsp;".CMealsFrontEndUtils::getCurrentMemberName()."<br/><br/>";
	}
	
}

?>